import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

const List<String> defaultImages = [
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2tdgve1j30ku0bsn75.jpg',
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2whp87sj30ku0bstec.jpg',
  'http://ww3.sinaimg.cn/large/006y8mN6ly1g6e2tl1v3bj30ku0bs77z.jpg',
];

class ADSwiperWidget extends StatelessWidget {
  final List<String> images;
  final int index;
  final double height;
  final int duration;
  final int autoplayDelay;
  final bool autoPlay;

  const ADSwiperWidget({
    super.key,
    this.images = defaultImages,
    this.index = 0,
    this.height = 288,
    this.duration = 500,
    this.autoplayDelay = 5000,
    this.autoPlay = true,
  });

  @override
  Widget build(BuildContext context) {
    var first = images[0];
    var isNetworkImage = false;
    if (first.startsWith('http')) isNetworkImage = true;
    return SizedBox(
      height: height,
      child: Swiper(
        index: index,
        itemBuilder: (context, index) {
          return isNetworkImage
              ? Image.network(
                  images[index],
                  fit: BoxFit.cover,
                )
              : Image.asset(
                  images[index],
                  fit: BoxFit.cover,
                );
        },
        itemCount: images.length,
        autoplay: autoPlay,
        pagination: const SwiperPagination(
          alignment: Alignment.bottomCenter,
          builder: SwiperPagination.dots,
        ),
        duration: duration,
        autoplayDelay: autoplayDelay,
      ),
    );
  }
}
